package com.clstu.generic;


import java.util.ArrayList;
import java.util.Comparator;

public class GenericExercise01 {
    public static void main(String[] args) {
        ArrayList<Employee> employees = new ArrayList<>();
        employees.add(new Employee("happy",12000,new MyDate(2002,8,11)));
        employees.add(new Employee("happy",13000,new MyDate(2001,7,23)));
        employees.add(new Employee("happy",14000,new MyDate(2002,7,3)));
        employees.sort(new Comparator<Employee>() {
            @Override
            public int compare(Employee o1, Employee o2) {
                return o1.getName().compareTo(o2.getName())==0?o1.getBirthday().compareTo(o2.getBirthday()) :
                        o1.getName().compareTo(o2.getName());
            }
        });
        for (Employee e:employees
             ) {
            System.out.println(e);
        }
    }
}
class Employee {
    private String name;
    private double sal;
    private MyDate birthday;

    public Employee(String name, double sal, MyDate birthday) {
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    public MyDate getBirthday() {
        return birthday;
    }

    public void setBirthday(MyDate birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", birthday=" + birthday ;
    }
}

class MyDate implements Comparable<MyDate>{//这里用了MyDate泛型之后后面就不用向下转型了
    private int year;
    private int month;
    private int day;

    public MyDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    @Override
    public String toString() {
        return
                "year=" + year +
                ", month=" + month +
                ", day=" + day
                ;
    }

    @Override
    public int compareTo(MyDate m) {
        return year-m.year == 0 ? month - m.month ==0 ? day-m.day :month-m.month : year - m.year ;
    }
}